package com.wanmait.recycle.util;

import java.io.File;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

public class UploadPhotosUtil {

	public static String getPhotoNameAndSave(String savePath,MultipartFile file,HttpServletRequest request)throws Exception {
		
		String photoName = null;
		if(!file.isEmpty()) {
			// 使用UUID给图片重命名，并去掉四个“-”
			String Name = UUID.randomUUID().toString().replaceAll("-", "");
			// 获取文件的扩展名
			String ext = FilenameUtils.getExtension(file.getOriginalFilename());  
			// 设置图片上传路径
			String url = request.getSession().getServletContext().getRealPath(savePath);
			// 以绝对路径保存重名命后的图片
			file.transferTo(new File(url + "/" + Name + "." + ext));
			photoName = Name+"."+ext;
		}
		return photoName;
	}
	
	
}
